package com.sumainfor.zuul.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.RRException;
import com.sumainfor.zuul.dao.SysRoleMenuDao;
import com.sumainfor.zuul.entity.SysRoleMenuEntity;
import com.sumainfor.zuul.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 角色与菜单对应关系
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Integer roleId, List<Integer> menuIdList) {
        //先删除角色与菜单关系
        deleteBatch(new Integer[]{roleId});

        if (menuIdList.size() == 0) {
            return;
        }

        //保存角色与菜单关系
        List<SysRoleMenuEntity> list = new ArrayList<>(menuIdList.size());
        for (Integer menuId : menuIdList) {
            SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
            sysRoleMenuEntity.setMenuId(menuId);
            sysRoleMenuEntity.setRoleId(roleId);

            list.add(sysRoleMenuEntity);
        }
        boolean state = this.insertBatch(list);

        if (state == false) {
            throw new RRException(JsonResult.actionFailure().getErrorMessage());
        }
    }

    @Override
    public List<Integer> queryMenuIdList(Integer roleId) {
        return baseMapper.queryMenuIdList(roleId);
    }

    @Override
    public int deleteBatch(Integer[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }

}
